# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: I_λ.py
  @time: 2022/3/19 14:38
  @desc:电流随功率的变化
"""
import time
from ATS.hardwarescan import conf_file_manager
from ATS.application.LCR_App.U2722A_App import U2722A_App
from ATS.application.LASER_App.L8164B_App import L8164B_App
from ATS.util.calcu_step_util import get_step_list
import matplotlib.pyplot as plt
import numpy as np

name = "SMU3"
u2722a_app = U2722A_App(name)

name = "SS3"
l8164b_app = L8164B_App(name)

"""
在测试之前需要进行稳偏的操作
"""





def ip_measure():
    u2722a_app.set_voltage_range(1)
    u2722a_app.set_current_range_limit("1mA", 0.001, 1)
    u2722a_app.set_current_nplc(1, 1)
    u2722a_app.set_voltage(3, 1)
    u2722a_app.output_state(1, 1)
    l8164b_app.set_wave_length(1310)
    l8164b_app.set_power_unit()
    power_list = get_step_list(5, 10, 1)
    # 8164的功率稳定时间
    stable_schedule = 10
    current_list = []
    for power in power_list:
        l8164b_app.set_power_value(float(power))
        time.sleep(stable_schedule)
        current = u2722a_app.get_current_value(1)
        current_list.append(current)
    u2722a_app.output_state(0, 1)
    return power_list, current_list


def plot_curve(x_data, y_data):
    plt.figure(figsize=(20, 8), dpi=80)
    plt.yscale('log')
    fig, ax = plt.subplots()
    ax.set_yscale('log')
    ax.plot(x_data, y_data, "b", label="1")
    ax.set_xlabel("WaveLength")
    ax.set_ylabel("Value")
    ax.grid(True)
    ax.legend()
    plt.show()




x_data, y_data = ip_measure()
plot_curve(x_data, y_data)


# def set_constant_voltage(voltage=3, i_range="1mA", i_compliance=0.001, channel=1):
#     u2722a_inst.write(f"VOLTage:RANGe R20V, (@{channel})")
#     u2722a_inst.write(f"CURRent:RANGe R{i_range},(@{channel})")
#     u2722a_inst.write(f"SENS:CURR:NPLC 1, (@{channel})")
#     cmds = [f"VOLT {voltage}, (@{channel})", f"CURR:LIM {i_compliance}, (@{channel})"]
#     for cmd in cmds:
#         u2722a_inst.write(cmd)
#     u2722a_inst.write(f"OUTPut 1, (@{channel})")


# def read_current(channel=1):
#     cmd = f"MEAS:CURR? (@{channel})"
#     return u2722a_inst.query(cmd)
#
#
# def init_8164(power_value=0):
#     n8164_inst.write("*cls")
#     n8164_inst.write(f"sour0:pow:unit 0")  # 修改power unit为db
#     n8164_inst.write(f"sour0:pow {power_value} dbm")
#
#
# def set_8164_wave(wave_length):
#     n8164_inst.write(f"sour0:wav {wave_length}NM")
#
#
# def output_on():
#     state = int(n8164_inst.query(f"OUTPUT:CHANNEL:STATE?"))  # on off
#     if not state:
#         n8164_inst.write("OUTPUT:STATE 1")


# start_wave = 1243
# stop_wave = 1350
# step_wave = 1
# points = list(range(start_wave, stop_wave, step_wave))
# points.append(stop_wave)
# print(len(points),points)
# start_time = time.time()
# set_constant_voltage()
# init_8164()
